<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="addFlow">
        添加子流程
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      border
    >
      <el-table-column
        prop="bindFlowName"
        label="关联流程名称"
      />
      <el-table-column
        prop="slot"
        label="标识"
      />
      <el-table-column
        label="操作"
        width="100"
        align="center"
      >
        <template slot-scope="{row}">
          <el-button type="success" @click="showEditForm(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑" :visible.sync="dialogEditVisible" custom-class="customWidth">
      <el-form ref="nodeDataForm" :rules="rules" :model="form" label-position="left" label-width="90px" style="width: 350px; margin-left:50px;">
        <el-form-item label="子流程" prop="bindFlowName">
          <el-input v-model="form.bindFlowName" readonly />
        </el-form-item>
        <el-form-item label="标识" prop="slot">
          <el-input v-model="form.slot" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="dialogEditVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="updateBindData">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="流程列表" :visible.sync="dialogFormVisible">
      <FlowList
        @onSelect="successData"
      />
    </el-dialog>
  </div>
</template>
<script>
import {
  DeleteFlowParams,
  GetBindFlowList,
  SaveFlowNodeParams,
  DeleteFlowNodeParams,
  SaveBindFlow
} from '@/api/SystemManagerApi'
import FlowList from '@/views/SystemSetting/SystemFlow/components/FlowList'
export default {
  rules: {
    slot: [{ required: true, message: 'type is required', trigger: 'change' }]
  },
  components: { FlowList },
  props: ['flowId', 'nodeId'],
  data() {
    return {
      form: {
        id: null,
        flowId: null,
        nodeId: null,
        bindFlowId: null,
        slot: null,
        bindFlowName: null
      },
      dialogFormVisible: false,
      dialogEditVisible: false,
      listLoading: false,
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      GetBindFlowList(this.flowId, this.nodeId).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    deleteFlowParams(flowId) {
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        DeleteFlowParams(flowId).then(() => {
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveFlowNodeParams(row) {
      this.listLoading = true
      if (row.status === 'Y') {
        SaveFlowNodeParams({
          nodeId: this.nodeId, paramsId: row.id
        }).then((response) => {
          this.dialogFormVisible = false
          this.getList()
        })
      } else {
        DeleteFlowNodeParams(this.nodeId, row.id).then((response) => {
          this.dialogFormVisible = false
          this.getList()
        })
      }
    },
    addFlow() {
      this.dialogFormVisible = true
    },
    successData(row) {
      this.dialogFormVisible = false
      SaveBindFlow({
        id: null,
        flowId: this.flowId,
        nodeId: this.nodeId,
        bindFlowId: row.id,
        slot: ''
      }).then((response) => {
        this.getList()
        this.dialogFormVisible = false
      })
    },
    showEditForm(row) {
      this.form = Object.assign({}, row)
      this.dialogEditVisible = true
    },
    updateBindData() {
      this.$refs['nodeDataForm'].validate((valid) => {
        if (valid) {
          SaveBindFlow(this.form).then((response) => {
            this.getList()
            this.dialogEditVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>
<style>

  .customWidth{
    width:500px
  }
</style>
